package com.wxg.regex;

import org.junit.Assert;
import org.junit.Test;

public class RegexTest {

	@Test
	public void testCmdPidFinder(){
		/**
		 * cmd /c netstat -aon | findstr 5000
		 */
		String str = null;
		str = "  TCP    0.0.0.0:5000           0.0.0.0:0              LISTENING       18948";
		//str = "  TCP    127.0.0.1:50000        0.0.0.0:0              LISTENING       20792";
		//str = "  TCP    127.0.0.1:56546        127.0.0.1:5000         ESTABLISHED     10508";
		boolean flag = false;
//		flag = str.matches(".*:5000\\s+.*");
		flag = str.matches(".*:(5000){1,}\\s+.*");
		Assert.assertEquals(true, flag);
		flag = str.matches(".*\\s+LISTENING\\s+.*");
		Assert.assertEquals(true, flag);
	}
	
	@Test
	public void testCmdPidFinder2(){
		/**
		 * cmd /c netstat -aon | findstr 5000
		 */
		int port = 5000;
		String str = null;
		str = "  TCP    0.0.0.0:5000           0.0.0.0:0              LISTENING       18948";
		//str = "  TCP    127.0.0.1:50000        0.0.0.0:0              LISTENING       20792";
		boolean flag = false;
//		flag = str.matches(".*:5000\\s+.*");
		flag = str.matches(".*:(" + port + "){1,}\\s+.*");
		Assert.assertEquals(true, flag);
	}
	
	@Test
	public void testFindPid(){
		String str = null;
		str = "  TCP    0.0.0.0:5000           0.0.0.0:0              LISTENING       18948";
		//str = "  TCP    127.0.0.1:50000        0.0.0.0:0              LISTENING       20792";
		String[] arr = null;
		arr = str.split("\\s+");
		Assert.assertEquals("18948", arr[arr.length-1]);
	}
	
}
